﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using RBS.Core.Extension;
using RBS.DBUtility;
using RBS.Common;
namespace RBS.Core.DAL
{
    //V_OrderDetails
    public partial class V_OrderDetails
    {

        public bool Exists(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from V_OrderDetails");
            strSql.Append(" where ");
            strSql.Append(" Id = @Id  ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(RBS.Core.Model.V_OrderDetails model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into V_OrderDetails(");
            strSql.Append("Id,State,OrderId,HCId,CName,Num,Price,DisAmount");
            strSql.Append(") values (");
            strSql.Append("@Id,@State,@OrderId,@HCId,@CName,@Num,@Price,@DisAmount");
            strSql.Append(") ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@State", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@HCId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Num", SqlDbType.Int,4) ,            
                        new SqlParameter("@Price", SqlDbType.Decimal,5) ,            
                        new SqlParameter("@DisAmount", SqlDbType.Decimal,9)   
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.State;
            parameters[2].Value = model.OrderId;
            parameters[3].Value = model.HCId;
            parameters[4].Value = model.CName;
            parameters[5].Value = model.Num;
            parameters[6].Value = model.Price;
            parameters[7].Value = model.DisAmount;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RBS.Core.Model.V_OrderDetails model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update V_OrderDetails set ");

            strSql.Append(" Id = @Id , ");
            strSql.Append(" State = @State , ");
            strSql.Append(" OrderId = @OrderId , ");
            strSql.Append(" HCId = @HCId , ");
            strSql.Append(" CName = @CName , ");
            strSql.Append(" Num = @Num , ");
            strSql.Append(" Price = @Price , ");
            strSql.Append(" DisAmount = @DisAmount  ");
 
            strSql.Append(" where Id=@Id  ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@State", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@HCId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Num", SqlDbType.Int,4) ,            
                        new SqlParameter("@Price", SqlDbType.Decimal,5) ,            
                        new SqlParameter("@DisAmount", SqlDbType.Decimal,9)  
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.State;
            parameters[2].Value = model.OrderId;
            parameters[3].Value = model.HCId;
            parameters[4].Value = model.CName;
            parameters[5].Value = model.Num;
            parameters[6].Value = model.Price;
            parameters[7].Value = model.DisAmount;
       
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            var strSql = new StringBuilder();
            strSql.Append("delete from V_OrderDetails ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)			};
            parameters[0].Value = Id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RBS.Core.Model.V_OrderDetails GetModel(int Id)
        {

            var strSql = new StringBuilder();
            strSql.Append("select Id, State, OrderId, HCId, CName, Num, Price, DisAmount ");
            strSql.Append("  from V_OrderDetails ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)			};
            parameters[0].Value = Id;


            RBS.Core.Model.V_OrderDetails model = new RBS.Core.Model.V_OrderDetails();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
             
            if (ds.Tables[0].Rows.Count > 0)
            {
                model = ds.Tables[0].Rows[0].ConvertToEntity<RBS.Core.Model.V_OrderDetails>();
                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM V_OrderDetails ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM V_OrderDetails ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        #region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * FROM V_OrderDetails ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion  ExtensionMethod


    }
}

